//
//  WXPToCPIListCell.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/9.
//
//

#import "WXPToCPIListCell.h"

@interface WXPToCPIListCell ()

@end

@implementation WXPToCPIListCell

#pragma mark - lazy
- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.userInteractionEnabled = true;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
    }
    return _viewBack;
}
- (UIImageView *)imageLogo{
    if (_imageLogo == nil) {
        _imageLogo = [UIImageView new];
        _imageLogo.widthHeight = XY(36, 36);
        [GlobalMethod setRoundView:_imageLogo color:[UIColor clearColor] numRound:6 width:0];
    }
    return _imageLogo;
}
- (UILabel *)labelName{
    if (_labelName == nil) {
        _labelName = [UILabel new];
        _labelName.textColor = COLOR_BLACK;
        _labelName.font = [UIFont boldSystemFontOfSize:14];
    }
    return _labelName;
}
-(UIButton *)btnApply{
    if (_btnApply == nil) {
        _btnApply = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnApply.tag = 1;
        [_btnApply addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btnApply.widthHeight = XY(Ratio(100), Ratio(30));
        [_btnApply setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnApply.backgroundColor = COLOR_PRIMARY;
        _btnApply.titleLabel.font = [UIFont systemFontOfSize:14];
        [GlobalMethod setRoundView:_btnApply color:[UIColor clearColor] numRound:Ratio(30)/2 width:0];
        [_btnApply setTitle:@"Solicitar" forState:(UIControlStateNormal)];
    }
    return _btnApply;
}
- (UILabel *)labelMoneyTitle{
    if (_labelMoneyTitle == nil) {
        _labelMoneyTitle = [UILabel new];
        _labelMoneyTitle.textColor = COLOR_CONTENT;
        _labelMoneyTitle.fontNum = 12;
    }
    return _labelMoneyTitle;
}
- (UILabel *)labelMoney{
    if (_labelMoney == nil) {
        _labelMoney = [UILabel new];
        _labelMoney.textColor = COLOR_BLACK;
        _labelMoney.font = [UIFont boldSystemFontOfSize:14];;
    }
    return _labelMoney;
}
- (UILabel *)labelTimeTitle{
    if (_labelTimeTitle == nil) {
        _labelTimeTitle = [UILabel new];
        _labelTimeTitle.textColor = COLOR_CONTENT;
        _labelTimeTitle.fontNum = 12;
    }
    return _labelTimeTitle;
}
- (UILabel *)labelTime{
    if (_labelTime == nil) {
        _labelTime = [UILabel new];
        _labelTime.textColor = COLOR_BLACK;
        _labelTime.font = [UIFont boldSystemFontOfSize:14];
    }
    return _labelTime;
}

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpView];
    }
    return self;
}

- (void)setUpView{
    [self.contentView addSubview:self.viewBack];
    [self.viewBack addSubview:self.imageLogo];
    [self.viewBack addSubview:self.labelName];
    [self.viewBack addSubview:self.btnApply];
    [self.viewBack addSubview:self.labelMoneyTitle];
    [self.viewBack addSubview:self.labelMoney];
    [self.viewBack addSubview:self.labelTimeTitle];
    [self.viewBack addSubview:self.labelTime];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            [GlobalMethod hitPointWithPageName:KEY_CpiProduct action:KEY_Click extInfo:@{@"id": [GlobalMethod formattedDouble:self.model.internalBaseClassIdentifier]}];
            NSString *urlString = readStr(self.model.externalLink);
            if(isStr(urlString)){
                NSURL *url = [NSURL URLWithString:urlString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                } else {
                    NSLog(@"Cannot open URL");
                }
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 刷新cell
- (void)resetCellWithModel:(ModelProductCpi *)model{
    self.model = model;
    self.viewBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    [self.imageLogo sd_setImageWithUrlStr:readStr(model.slogan) placeholderImage:@""];
    self.imageLogo.leftTop = XY(LEFT_MARDIN, 16);
    
    [self.labelName fitTitle:readStr(model.name) variable:0];
    self.labelName.leftCenterY = XY(self.imageLogo.right+12, self.imageLogo.centerY);
    
    self.btnApply.rightCenterY = XY(self.viewBack.width-self.imageLogo.left, self.labelName.centerY);
    
    [self.labelMoneyTitle fitTitle:@"Importe" variable:0];
    self.labelMoneyTitle.leftTop = XY(self.imageLogo.left, self.imageLogo.bottom+16);
    
    [self.labelMoney fitTitle:[NSString stringWithFormat:@"$ %@",[GlobalMethod showMoney:strDot2F(model.maxAmount)]] variable:0];
    self.labelMoney.leftTop = XY(self.imageLogo.left, self.labelMoneyTitle.bottom+6);
    
    [self.labelTimeTitle fitTitle:@"Plazo" variable:0];
    self.labelTimeTitle.rightCenterY = XY(self.btnApply.right, self.labelMoneyTitle.centerY);
    
    [self.labelTime fitTitle:[NSString stringWithFormat:@"%@ Días", strDotF(model.term)] variable:0];
    self.labelTime.rightCenterY = XY(self.btnApply.right, self.labelMoney.centerY);
    
    self.viewBack.height = self.labelMoney.bottom+16;
    self.viewBack.centerXTop = XY(SCREEN_WIDTH/2, LEFT_MARDIN);
    self.height = self.viewBack.bottom;
}

@end
